<?php
namespace app\controller;

use think\Request;
use app\controller\Base;
use app\common\service\UtilService;
use app\model\EaWxPlatform;
use app\model\EaWxPrograms;
use app\model\EaWxSite;
use app\model\EaWxCompany;
use Michelf\MarkdownExtra;


class Index extends Base
{
    /**
     * [index 首页]
     * @since  2025-05-29
     * @author Dai哥
     * @param  Request    $request [description]
     * @return [type]              [description]
     */
    public function index(Request $request)
    {

        $arrs = [];

        # 公众号分类
        $arrs['pla']      = EaWxPlatform::plaGroupName();

        # 小程序
        $arrs['programs'] = EaWxPrograms::programsGroupName();

        # 网站
        $arrs['site']     = EaWxSite::siteGroupName();

        # 最新收录16个网站
        $arrs['site_new'] = EaWxSite::field('id, title')->where('status', 1)->order('dates DESC, id DESC')->limit(16)->cache(600)->select()->toArray();

        foreach ($arrs['site'] as $key => $value) {

            $list = EaWxSite::where('status', 1)
                ->field('id, title, icon')
                ->where('type2', $value['type'])
                ->order('views desc')
                ->cache(600)
                ->limit(5)
                ->select()
                ->toArray();

            $arrs['site'][$key]['item'] = $list;
        }

        # 网站价值排行
        $arrs['site_view'] = EaWxSite::field('id, title, views')->where('status', 1)->order('views DESC')->limit(11)->cache(600)->select()->toArray();

        # 最新的企业
        $arrs['company_new'] = EaWxCompany::field('id, title, star_time')->limit(15)->order('star_time DESC')->cache(600)->select()->toArray();

        # 最新公众号列表
        $arrs['pla_new'] = EaWxPlatform::field('id, name, views, auth, type, avatar')->where('status', 1)->cache(600)->order('dates DESC')->limit(23)->select()->toArray();

        # 最新小程序列表
        $arrs['programs_new'] = EaWxPrograms::field('id, name, views, type, avatar')->where('status', 1)->cache(600)->order('dates DESC')->limit(23)->select()->toArray();

        # 统计
        $arrs['pla_total'] = EaWxPlatform::where('status', 1)->cache(800)->count();
        $arrs['programs_total'] = EaWxPrograms::where('status', 1)->cache(800)->count();
        $arrs['site_total'] = EaWxSite::where('status', 1)->cache(800)->count();
        $arrs['company_total'] = EaWxCompany::cache(800)->count();

        return view('index', compact('arrs'));
    }

    public function liushi(Request $request)
    {
        return view('liushi');
    }

    public function liushi_res(Request $request)
    {

        // while (ob_get_level())
        // ob_get_level()
        // ob_end_clean();
        
        header('Content-Type: text/event-stream'); // 或 text/plain
        header('Cache-Control: no-cache');
        header('X-Accel-Buffering: no'); // 禁用Nginx缓冲

        // 模拟流式数据生成
        for ($i = 1; $i <= 10; $i++) {
            $data = "数据块 {$i} " . str_repeat('.', rand(5, 20)) . "<br>";
            
            // 输出数据（按分块编码格式）
            echo $data;
            
            // 立即刷新输出缓冲区
            ob_flush();
            flush();
            
            // 模拟处理延迟
            sleep(1);
        }
    }
}
